package com.wsl.message;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: wsl
 * @Date: 2022/2/15 14:30
 */
public class MessageUtil {

    public static void writeMessage(HttpServletResponse response, Map map){
        try {
            PrintWriter pw = response.getWriter();
            ObjectMapper mapper = new ObjectMapper();
            pw.write(mapper.writeValueAsString(map));
            pw.flush();
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /*返回信息 msg status obj*/
    public String write(String msg, String status, Object obj) {
        try {
            Map map = new HashMap<Object, Object>();
            ObjectMapper mapper = new ObjectMapper();
            map.put("msg", msg);
            map.put("obj", obj);
            map.put("status", status);
            return mapper.writeValueAsString(map);
        } catch (Exception e) {
            e.printStackTrace();
            return error();
        }
    }

    public String write(StatusCode statusCode,Object obj){
        try {
            Map map = new HashMap<Object, Object>();
            ObjectMapper mapper = new ObjectMapper();
            map.put("msg", statusCode.getMessage());
            map.put("obj", obj);
            map.put("status", statusCode.getCode());
            return mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return error();
        }
    }

    public String write(StatusCode statusCode){
        Map map = new HashMap<Object, Object>();
        ObjectMapper mapper = new ObjectMapper();
        try {
            map.put("msg", statusCode.getMessage());
            map.put("status", statusCode.getCode());
            return mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return error();
        }
    }

    public String write(String msg, int status, Object obj) {
        try {
            Map map = new HashMap<Object, Object>();
            ObjectMapper mapper = new ObjectMapper();
            map.put("msg", msg);
            map.put("obj", obj);
            map.put("status", status);
            return mapper.writeValueAsString(map);
        } catch (Exception e) {
            e.printStackTrace();
            return error();
        }
    }

    /*返回信息 msg  status*/
    public String write(String msg, String status) {
        try {
            Map map = new HashMap<Object, Object>();
            ObjectMapper mapper = new ObjectMapper();
            map.put("msg", msg);
            map.put("status", status);
            return mapper.writeValueAsString(map);
        } catch (Exception e) {
            e.printStackTrace();
            return error();
        }
    }

    /*返回信息 msg  status*/
    public String write(String msg, int status) {
        try {
            Map map = new HashMap<Object, Object>();
            ObjectMapper mapper = new ObjectMapper();
            map.put("msg", msg);
            map.put("status", status);
            return mapper.writeValueAsString(map);
        } catch (Exception e) {
            e.printStackTrace();
            return error();
        }
    }

    /*请求失败信息*/
    public String error() {
        try {
            Map map = new HashMap<Object, Object>();
            ObjectMapper mapper = new ObjectMapper();
            map.put("msg",StatusCode.ERROR.getMessage());
            map.put("status", StatusCode.ERROR.getCode());
            return mapper.writeValueAsString(map);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}